package com.mk.service.platform;

import act.controller.Controller;
import com.github.d925529.apidoc.ApiHttpMethod;
import com.github.d925529.apidoc.annotation.*;
import com.mk.domain.TOrderDetail;
import com.mk.interceptor.ITranslate;
import com.mk.service.BasePaasService;
import com.mk.util.MapUtil;
import com.mk.util.S2;
import org.beetl.sql.core.engine.PageQuery;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;
import org.osgl.mvc.annotation.PutAction;
import org.osgl.mvc.annotation.With;
import org.osgl.util.C;
import org.osgl.util.S;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明：订单明细表后台服务
 * 作者：292303959@qq.com 
 * 日期：2018-10-17 10:5:47 
 * 版本：1.0.0
 */
@Controller("tOrderDetail")
@Api(group = "pl",title = "订单明细表服务", path = "/tOrderDetail")
@SuppressWarnings("unused")
public class TOrderDetailService extends BasePlatformService {
    @Inject
    private TOrderDetail.Mapper tOrderDetailMapper;
    @Inject
    private BasePaasService basePaasService;


    @With({ILogin.class, ITranslate.class})
    @PostAction("add.json")
    @ApiMethod(title = "增加保存订单明细表", path = "/add.json", method = ApiHttpMethod.POST)
    @ApiParam(name = "tOrderDetail", title = "订单明细表", type = TOrderDetail.class)
    @ApiReturn(description = "主键", type = String.class)
    public String add(@NotNull TOrderDetail tOrderDetail) {
        String id = S2.UUID();
        tOrderDetail.setOdid(id);
        tOrderDetailMapper.insertTemplate(tOrderDetail);
        return id;
    }


    @With({ILogin.class, ITranslate.class})
    @PutAction("update.json")
    @ApiMethod(title = "修改保存订单明细表", path = "/update.json", method = ApiHttpMethod.PUT)
    @ApiParam(name = "tOrderDetail", title = "订单明细表", type = TOrderDetail.class)
    @ApiReturn(description = "修改了的数量", type = int.class)
    public int update(@NotNull TOrderDetail tOrderDetail) {
        return tOrderDetailMapper.updateTemplateById(tOrderDetail);
    }

    @With(ILogin.class)
    @GetAction("view.json")
    @ApiMethod(title = "查询指定订单明细表", path = "/view.json", method = ApiHttpMethod.GET)
    @ApiParam(name = "id", title = "订单明细表的主键", type = String.class)
    @ApiReturn(description = "订单明细表", type = TOrderDetail.class)
    public TOrderDetail view(@NotNull String id){
        return tOrderDetailMapper.single(id);
    }



    @With(ILogin.class)
    @GetAction("list.json")
    @ApiMethod(title = "查询指定订单明细表", path = "/list.json", method = ApiHttpMethod.GET)
    @ApiParam(name = "orderid", title = "订单ID", type = String.class)
    @ApiReturn(description = "订单明细表", type = TOrderDetail.class)
    public PageQuery<Map> list(String currentPage,
                                   String pageSize,
                                   String sortName,
                                   String sortType) {
        Map<String, String[]> allParam = context.allParams();
        Map<String, String[]> allParams = new HashMap<>();
        for (Map.Entry<String, String[]> p : allParam.entrySet()) {
            allParams.put(p.getKey(), p.getValue());
        }
        return basePaasService.queryBase("tOrderDetail.select_list_byOrderId", currentPage, pageSize, sortName, sortType, allParams);
    }
    @With(ILogin.class)
    @PostAction("afterSale.json")
    @ApiMethod(title = "售后", path = "/afterSale.json", method = ApiHttpMethod.POST)
    @ApiParam(name = "postData", title = "明细信息", type = String.class)
    @ApiReturn(description = "更新的条数", type = Integer.class)
    public int afterSale(List<Map<String,Object>> postData) {
        postData.forEach(map->{
            Integer afternum= MapUtil.getInt(map,"afternum");
            if(null!=afternum&&afternum>0){
                String odid=MapUtil.getStr(map,"odid");
//                更新售后数量和奖励金额
                tOrderDetailMapper.updateAfterSalenum(odid,afternum);
            }
        });
        return 0;
    }




}

